<template>
  <li v-if="divider"
      class="my-contextmenu__divider"></li>
  <li v-else
      class="my-contextmenu__item"
      :class="classes"
      @mouseenter="handleMouseEnter"
      @mouseleave="handleMouseLeave"
      @click.prevent.stop="handleClick">
    <div class="my-contextmenu__left">
    <span class="my-contextmenu__icon">
      <i class="my-icon" :class="icon"></i>
    </span>
      <span class="my-contextmenu__label">{{label}}</span>
    </div>
    <div class="my-contextmenu__right">
      <span v-if="info" class="my-contextmenu__info">{{info}}</span>
      <i v-if="hasChild" class="my-contextmenu__arrow el-icon-caret-right"></i>
    </div>
    <slot v-if="active" :active="active" :children="children"></slot>
  </li>

</template>

<script>

export default {
  components: {
  },
  props: {
    icon: [String, Object],
    label: String,
    info: String,
    disabled: Boolean,
    divider: Boolean,
    children: Array
  },
  data() {
    return {
      active: false
    }
  },
  computed: {

    classes() {
      return {
        'is-disabled': this.disabled,
        'is-has-child': this.hasChild
      }
    },
    hasChild() {
      return this.children && this.children.length > 0
    }
  },
  methods: {
    handleMouseEnter() {
      this.active = true
      if (this.hasChild) {
        this.$nextTick(() => {
          this.$emit('submenu', this)
        })
      }

    },
    handleMouseLeave() {
      this.active = false
    },
    handleClick() {
      if (this.disabled) {
        return
      }
      this.$emit('click', this)

    }
  }
}
</script>
<style lang="less" scoped>
.my-contextmenu {
  &__item {
    position: relative;
    padding: 0 8px;
    height: 30px;
    line-height: 30px;
    cursor: pointer;
    clear: both;
    color: rgba(0,0,0,.85);
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;

    &:hover {
      background: #1890ff;
      color: #e6f7ff;
    }
  }

  &__icon {
    width: 20px;
    display: inline-block;
    vertical-align: top;
  }
  &__label {
    margin-right: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    vertical-align: top;
  }
  &__arrow {
    margin-right: -4px;
  }
  &__divider {
    height: 0;
    border-top: 1px solid rgba(0,0,0,.09);
    margin: 2px 0;
    clear: both;
  }
}


</style>

